home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / pascal / sprites.zip / SPRITES.PAS < prev    next >
Pascal/Delphi Source File  |  1991-04-28  |  15KB  |  504 lines

  1. unit Sprites;
  2. { Basically a simple and effective spriteengine for use with Turbo }
  3. { Pascal 6.0. (Does not require the GRAPH unit..)                  }
  4. {                                                                  }
  5. { Designed for use with MCGA, VGA and compatibles. Works in mode   }
  6. { mode $13 (320x200 with 256 simultaneously colours.               }
  7. {                                                                  }
  8. { Written by:                                                      }
  9. {            Marius Kjeldahl                                       }
  10. {            Stud. post 104                                        }
  11. {            N-7034 Trondheim - NTH                                }
  12. {            Norway                                                }
  13. {            Ph. +47 7 58 91 11                                    }
  14. {            e-mail: mariusk@lise.unit.no                          }
  15. {                    (at NTH - Norwegian Institute of Technology ) }
  16. {                    (dept. of Business and Information Technolgy) }
  17. {                                                                  }
  18. { These routines are being distributed as shareware. To be used in,}
  19. { or as part of any commercial product, you have to become a       }
  20. { registered user. As a registered user you will receive upgrades  }
  21. { and rights to distribute these routines with your products.      }
  22. { To become a registered user, you will have to send a letter with }
  23. { who you are and what product(s) will use these routines and      }
  24. { make US$39 payable to the author (cheque or money..).            }
  25. {                                                                  }
  26. {                                                                  }
  27. { If you have any suggestions or comments please do not hesitate   }
  28. { to contact me. Have fun...                                       }
  29. {                                                                  }
  30. { Future plans for enhancements: interrupt driven, faster rep      }
  31. { movsw for sprites, built in animation, screen region scrolling   }
  32. { and more..                                                       }
  33.  
  34. interface
  35. uses
  36.   Dos;
  37. const
  38.   MaxSprites = 14; { Maximum number of sprites activated simultaneously }
  39.   MaxDim = 25*25;  { Dimensions of largest sprite to be used (x*y)      }
  40. type
  41.   ScreenTypePointer = ^ScreenType;       { Pointer to a virtual screen  }
  42.   ScreenType = array [1..64000] of byte; { Array to hold virtual screen }
  43.   SpriteType = record                    { Misc. sprite data            }
  44.                  oldx, oldy,             { - old location               }
  45.                  x, y : integer;         { - current location           }
  46.                  w, h : byte;            { - width and height           }
  47.                  SpriteData,             { - spriteimage                }
  48.                  Buffer : array [0..MaxDim-1] of byte; { spritebackgr.  }
  49.                  Active : boolean;       { - currently active           }
  50.                  ix, iy : integer;       { - sprite increment           }
  51.                                          {   (not currently used)       }
  52.                end;
  53. var
  54.   Sprite : array [1..MaxSprites] of SpriteType; { Array of sprites      }
  55.   Virtual_Screen : ScreenTypePointer;    { Pointer to virtual screen    }
  56.  
  57.   procedure DrawSprites;
  58.   { Saves background and draws all currently active sprites at their    }
  59.   { current location.                                                   }
  60.  
  61.   procedure LoadSprite (Num : byte; FileName : string);
  62.   { Loads spritedata from ordinary text file. Examine the .SPR files    }
  63.   { for further details. Use .CEL files instead if you've have          }
  64.   { purchased AutoDesk Animator...                                      }
  65.  
  66.   procedure SetMode (Mode : word);
  67.   { Sets screen mode. Use $13 for use with sprites                      }
  68.  
  69.   procedure ShowVirtualScreen ;
  70.   { Copies the VirtualScreen to the users screen. Any changes done with }
  71.   { the sprites and/or their position will NOT be visible until this    }
  72.   { routine has been called!                                            }
  73.  
  74.   procedure LoadCOL (FileName : string);
  75.   { Loads a file containing the palette desc. of the 256 colours and    }
  76.   { programs the VGA/MCGA to use these palette. It uses AutoDesk        }
  77.   { Animators file format - so you can use Animator to select colors and}
  78.   { then save the palette in Animators ordinary .COL file.              }
  79.   { For those without Animator (you should not be..) the file format is }
  80.   { simple. Each colour (from 0 to 255) has three bytes which containts }
  81.   { red, green and blue values. First in the file comes (usually)       }
  82.   { 0,0,0 - black and so on until the last colour.                      }
  83.  
  84.   procedure LoadCEL (FileName :  string; ScrPtr : pointer);
  85.   { Directly loads a CEL to the location pointed to by ScrPtr.          }
  86.   { This routine uses AutoDesk Animators file format. This means you    }
  87.   { can use Animators excellent drawing tools to design you sprites and }
  88.   { save them in a ordinary .CEL file.                                  }
  89.   { For those without Animator; here is a short desc. of the format:    }
  90.   { The first 800 bytes is Animators header. It does include various    }
  91.   { information like it's own colours palette, width and height.        }
  92.   { However this version skips all that information and just reads the  }
  93.   { image data into the location pointed to by ScrPtr. Remember to      }
  94.   { set the sprites width and height too! (It is NOT read you of the    }
  95.   { .CEL file in this release..                                         }
  96.  
  97.   procedure DisableAllSprites;
  98.   { Disables all sprites. But this routine does not restore the screen  }
  99.   { image (compare with HideSprites..                                   }
  100.  
  101.   procedure HideSprites;
  102.   { Disables all sprites. Basically same as DisableAllSprites, but this }
  103.   { routine also recovers the "original" screen image.                  }
  104.  
  105.   procedure FillBox (x1, y1, x2, y2 : integer; b : byte);
  106.   { Draws a coloured box with upper left corner x1,y1 and lower right   }
  107.   { corner x2,y2.                                                       }
  108.  
  109.   procedure CopySprite (var Sprite : SpriteType; x1, y1 : integer);
  110.   { "Stamps" a copy of any sprite at the chosen location x1,y1.         }
  111.   { Use this routine if you want to put the image there, but do not plan}
  112.   { to animate in anyway..                                              }
  113.  
  114.   procedure WaitForVerticalRetrace;
  115.   { Waits for vertical retrace. Will be used in further releases..      }
  116.  
  117. implementation
  118.  
  119. procedure CopySprite (var Sprite : SpriteType; x1, y1 : integer); assembler;
  120. label
  121.   _Redraw, _DrawLoop, _Exit, _LineLoop, _NextLine, _Store, _NoPaint;
  122.   asm
  123.     push  ds
  124.     push  es
  125.     lds   si,Sprite
  126.     mov   ax,x1     { ax = x }
  127.     mov   bx,y1     { bx = y }
  128. _Redraw:
  129.     push  ax
  130.     push  bx
  131.     mov   ax,word(Virtual_Screen+2)
  132.     mov   es,ax         { ES=A000h }
  133.     pop   bx            { ax = y }
  134.     mov   ax,320
  135.     mul   bx            { ax = y * 320 }
  136.     pop   bx            { ax = x }
  137.     add   ax,bx         { bx = bx + ax dvs. skjermadr.. }
  138.     mov   di,ax         { di = skjermadr. }
  139.     mov   dl,[si+9]     { dl = height of sprite }
  140.     xor   ch,ch
  141.     mov   cl,[si+8]     { cx = width of sprite }
  142.     add   si,10         { si = start of spritedata }
  143.     cld
  144. _DrawLoop:
  145.     push  di            { store y adr. for later }
  146.     push  cx            { store width }
  147. _LineLoop:
  148.     mov   bl,byte ptr [si]
  149.     or    bl,bl
  150.     jnz   _Store
  151. _NoPaint:
  152.     inc    si
  153.     inc    di
  154.     loop   _LineLoop
  155.     jmp    _NextLine
  156. _Store:
  157. {    test   byte ptr [es:di],1
  158.     jz     _NoPaint}
  159.     movsb
  160.     loop  _LineLoop
  161. _NextLine:
  162.     pop   cx
  163.     pop   di
  164.     dec   dl
  165.     jz    _Exit
  166.     add   di,320        { di = next line of sprite }
  167.     jmp   _DrawLoop
  168. _Exit:
  169.     pop   es
  170.     pop   ds
  171.   end;
  172.  
  173. procedure DrawSprite (var Sprite : SpriteType); assembler;
  174. label
  175.   _Redraw, _DrawLoop,